package com.pang.security.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.StreamSupport;

/**
 * @version 1.0
 * 功能目的描述 ：用于 封装需要范湖的结果        ========>>>>>>>>>>>
 * @author： pang-yun
 * @date： 2021-06-23 10:24
 */

@Data
@Component
public class Result {

    //是否成功
    private Boolean success;
    //返回码
    private Integer code;
    //返回信息
    private String message;
    //返回数据
    private Map<String, Object> data = new HashMap<>();

    /*
     * 构造方法私有，里面的方法都是静态方法，达到保护属性的作用*/
    private Result() {
    }

    /*使用链式编程*/
    public static Result ok() {
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        return result;
    }

    public static Result error(){
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(ResultCode.COMMON_FAIL.getCode());
        result.setMessage(ResultCode.COMMON_FAIL.getMessage());
        return result;
    }

    public static Result error(ResultCode resultCode){
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        return result;
    }


    /*自定义返回成功与否*/
    public Result success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public Result message(String message){
        this.setMessage(message);
        return this;
    }
    public Result code(Integer code){
        this.setCode(code);
        return this;
    }

    public Result data(Map<String,Object> map){
        this.setData(map);
        return this;

    }
}
